<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Item as ItemModel;
use app\admin\model\AuthGroupAccess;
use app\admin\model\User;
use think\Db;

class Item extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new ItemModel;      //别名：避免与控制名冲突
    }
    
    public function index()
    {
        $AuthGroupAccess = new AuthGroupAccess();
        $auth = $AuthGroupAccess->where('uid', $_COOKIE['uid'])->field('group_id')->find();
        $where = [];
        if (input('get.search')){
            $where['title'] = ['like', '%'.input('get.search').'%'];
        }
        $order = 'id asc';
        $dataList = $this->cModel->where($where)->order($order)->field($field)->paginate('', false, page_param());
        foreach ($dataList as $k => $value){
            if ($auth['group_id'] < 2) {
                $User = new User();
                $data = $User->where('id', $value['person_id'])->field('name')->find();
                $dataList[$k]['person_name'] = $data['name'];
            }
            if ($auth['group_id'] > 2 && $value['person_id'] == $_COOKIE['uid'] || strpos($value['whole_id'],$_COOKIE['uid']) !== false ) {
                $User = new User();
                $data = $User->where('id', $value['person_id'])->field('name')->find();
                $dataList[$k]['person_name'] = $data['name'];
            }else{
                unset($dataList[$k]);
            }
        }
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    
    public function create()
    {
        if (request()->isPost()){
            $data = input('post.');
            $data['whole_id'] =  implode(",", $data['whole_id']);
            $data['create_time'] = time();
            $data['stime'] = strtotime($data['stime']);
            $data['otime'] = strtotime($data['otime']);
            $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $field = 'id,name';
            $sale = Db::name('user')->field($field)->where('mod_id',4)->order($order)->select();
            $this->assign('sale', $sale);
            $field = 'id,name';
            $where['mod_id'] = array(['=',1],['=',2],'or');
            $kai = Db::name('user')->field($field)->where($where)->order($order)->select();
            $this->assign('kai', $kai);
            return $this->fetch('create');
        }
    }
    
    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
            $data['stime'] = strtotime($data['stime']);
            $data['otime'] = strtotime($data['otime']);
            $data['update_time'] = time();
            $result = $this->cModel->validate(CONTROLLER_NAME.'.edit')->save($data, $data['id']);
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            $data = $this->cModel->get($id);
            $data->Users;
            $this->assign('data', $data);
            return $this->fetch();
        }
    }
    
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                $id_arr = explode(',', $id);
                if(in_array(20, $id_arr) || in_array(21, $id_arr) || in_array(22, $id_arr)){
                    return ajaxReturn(lang('not_edit'));
                }
                $where = [ 'id' => ['in', $id_arr] ];
                $result = $this->cModel->where($where)->delete();
                if ($result){
                    return ajaxReturn(lang('action_success'), url('index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }
    }
    
}